/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Layered_Process_Audit",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.DomainModels;
using ATkg.BizAppProject.IServices;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;

namespace ATkg.BizAppProject.Controllers
{
    [Route("api/[controller]/[action]")]

    public partial class Layered_Process_AuditController
    {
        private readonly ILayered_Process_AuditService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public Layered_Process_AuditController(
            ILayered_Process_AuditService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }
        // 前端提交新建数据模型（改进版）
        public class AuditSubmitModel
        {
            public string LevelLabel { get; set; }
            public string ProductionLine { get; set; }
            public string ResponsiblePerson { get; set; }
            public string FinalResult { get; set; }
            public List<CheckItem> CheckResults { get; set; }
            //public long Id { get; set; }
        }
        // 前端提交整改数据模型（改进版）
        public class AuditCorrectedModel
        {
            public string LevelLabel { get; set; }
            public string ProductionLine { get; set; }
            public string ResponsiblePerson { get; set; }
            public string FinalResult { get; set; }
            public List<CheckItem> CheckResults { get; set; }
            public long Id { get; set; }
        }
        // 前端提交查询年月级别审核记录
        public class SearchContet
        {
            public string LevelLabel { get; set; }
            public int year { get; set; }
            public int month { get; set; }

        }
        // 检查项详细模型（用于解析JSON）
        public class CheckItem
        {
            public string category { get; set; }
            public string number { get; set; }
            public string name { get; set; }
            public string result { get; set; } // pass/fail/corrected/na
            public string comment { get; set; }
            public List<string> nonconformityPhotos { get; set; } = new List<string>();
            public string rectificationPerson { get; set; }
            public string rectificationComment { get; set; }
            public List<string> rectificationPhotos { get; set; } = new List<string>();
        }
        // 将网页中填写的审核信息传入数据库
        [HttpPost("GetInfoFromWeb")]
        [AllowAnonymous]
        public async Task<JsonResult> GetInfoFromWeb([FromBody] AuditSubmitModel model)
        {
            try
            {
                //JArray houduan = JArray.Parse(json_data);
                //Dictionary<object, object> dataFromWeb = houduan[0].ToObject<Dictionary<object, object>>();

                var r = await Service.DealDataFromWeb(model);
                return new JsonResult(new
                {
                    Success = true,
                    Message = "提交成功！",
                    Data = r  // 新增：返回string格式的时间
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

        // 获取数据库中  审核总次数、今日审核、待整改项、已完成整改  这4个数在主页面中进行展示
        [HttpPost("GetInfoFromDB")]
        [AllowAnonymous]
        public async Task<JsonResult> GetInfoFromDB([FromBody] SearchContet model)
        {
            try
            {
                var r = await Service.GetDataFromDB(model);
                return new JsonResult(new
                {
                    Success = true,
                    Data = r  
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

        // 获取数据库中所有NG(待整改)的信息，返回主要信息进行简要展示，在待整改页面中进行展示
        [HttpPost("GetNgResultsData")]
        [AllowAnonymous]
        public async Task<JsonResult> GetNgResultsData([FromBody] SearchContet model)
        {
            try
            {
                var r = await Service.GetNgResultsData(model);
                return new JsonResult(new
                {
                    Success = true,
                    Data = r
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }
        // 获取数据库中所有Corrected(已整改)的信息，返回主要信息进行简要展示，在待整改页面中进行展示
        [HttpPost("GetCorrectedResultsData")]
        [AllowAnonymous]
        public async Task<JsonResult> GetCorrectedResultsData([FromBody] SearchContet model)
        {
            try
            {
                var r = await Service.GetCorrectedResultsData(model);
                return new JsonResult(new
                {
                    Success = true,
                    Data = r
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

        // 将网页中填写的审核信息传入数据库
        [HttpPost("DealCorrectedDataFromWeb")]
        [AllowAnonymous]
        public async Task<JsonResult> DealCorrectedDataFromWeb([FromBody] AuditCorrectedModel model)
        {
            try
            {
                //JArray houduan = JArray.Parse(json_data);
                //Dictionary<object, object> dataFromWeb = houduan[0].ToObject<Dictionary<object, object>>();

                var r = await Service.DealCorrectedDataFromWeb(model);
                return new JsonResult(new
                {
                    Success = true,
                    Message = "提交成功！",
                    Data = r  // 新增：返回string格式的时间
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

        // 获取数据库中的工段名和相关人员
        [HttpPost("GetInfoFromDbPeopleManage")]
        [AllowAnonymous]
        public async Task<JsonResult> GetInfoFromDbPeopleManage()
        {
            try
            {


                var r = await Service.GetInfoFromDbPeopleManage();
                return new JsonResult(new
                {
                    Success = true,
                    Message = "获取成功！",
                    Data = r  // 新增：返回string格式的时间
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

    }
}
